package com.fs.web;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.fs.pojo.Paper;
import com.fs.pojo.Questions;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.fs.pojo.ClassPaper;
import com.fs.pojo.Paper;
import com.fs.service.ClassPaperService;
import com.fs.service.PaperService;
import com.fs.utils.BeanFactories;
import com.fs.utils.JsonData;
import com.fs.utils.ResponseJSONUtil;
import com.fs.vo.ClassPaperVO;
import com.fs.vo.CourseClassVO;
import org.apache.commons.io.IOUtils;
import org.apache.commons.io.IOUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

@WebServlet("/paper/*")
public class PaperServlet extends HttpServlet {

    private PaperService paperService = BeanFactories.getBean(PaperService.class);
    private ClassPaperService classPaperService = BeanFactories.getBean(ClassPaperService.class);
    private HttpServletRequest req;
    private HttpServletResponse resp;

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        response.setContentType("application/json;charset=utf-8");
        request.setCharacterEncoding("utf-8");

        this.req = request;
        this.resp = response;

        switch (req.getPathInfo()) {
            case "/getAllHomeWorkPapersByClassId": getAllHomeWorkPapersByClassId(); break;
            case "/getAllExamPapersByClassId": getAllExamPapersByClassId(); break;
            case "/addNewPaper": addNewPaper(); break;
            case "/getAllHomeWorkAndExamByTeacherId": getAllHomeWorkAndExamByTeacherId();break;
            case "/deleteById": deleteById();break;
            case "/deleteAll": deleteAll();break;
            case "/qurycourseNameAndClassByTeacherId" :qurycourseNameAndStudentByTeacherId(); break;
            case "/addClassPaper":addClassPaper(); break;
        }

    }



    /**
     * 添加教室试卷
     * @throws IOException
     */
    private void addClassPaper() throws IOException {
        String s = IOUtils.toString(req.getInputStream(), "utf-8");
        JSONObject jsonObject = JSONObject.parseObject(s);
        JSONArray classIds  =jsonObject.getJSONArray("classId");
        String paperId = jsonObject.getString("paperId");
        String publishName = jsonObject.getString("publishName");
        String deadTime = jsonObject.getString("deadTime");
        String durtion = jsonObject.getString("durtion");
        String type = jsonObject.getString("type");
        List<ClassPaper> Classpapers = new ArrayList<>();
        for (int i = 0; i <classIds.size() ; i++) {
            ClassPaper classPaper = new ClassPaper();
            classPaper.setClassId((String)classIds.get(i));
            classPaper.setDeadTime(deadTime);
            classPaper.setType(Integer.parseInt(type));
            classPaper.setDuration(Integer.parseInt(durtion));
            classPaper.setPaperId(paperId);
            classPaper.setPublishName(publishName);
            Classpapers.add(classPaper);
        }
        classPaperService.addClassPaper(Classpapers);
        resp.getWriter().print(JSONObject.toJSONString(new JsonData(1,"","发布成功!")));
    }

    /**
     * 通过教师ID查找他所交课程下的所有班级
     * @throws IOException
     */
    private void qurycourseNameAndStudentByTeacherId() throws IOException {
        String teacherId = req.getParameter("teacherId");
        List<CourseClassVO> list =  paperService.qurycourseNameAndClassByTeacherId(teacherId);
        ResponseJSONUtil.responseJSON(JsonData.buildSuccess(list),resp);
    }

    /**
     * 选中删除
     */
    private void deleteAll() throws IOException {
        String id = req.getParameter("id");
        int i = paperService.deleteById(id);
        ResponseJSONUtil.responseJSON(JsonData.buildSuccess(i),resp);
    }

    /**
     * 表内单个删除
     */
    private void deleteById() throws IOException{
        String id = req.getParameter("id");
        int i = paperService.deleteById(id);
        JsonData data;
        if(i>0){
            data = new JsonData(1,"","删除成功");
        }else {
            data = new JsonData(0,"","删除失败");
        }
        resp.getWriter().write(JSONObject.toJSONString(data));
    }

    /**
     * 获取该老师下的所有列表任务
     */
    private void getAllHomeWorkAndExamByTeacherId() throws IOException {

        String teacherId = req.getParameter("teacherId");
        List<Paper> papers = paperService.queryByTeacherId(teacherId);
        ResponseJSONUtil.responseJSON(JsonData.buildSuccess(papers),resp);

    }

    /**
     * 通过班级id获取发布的作业试卷
     */
    private void getAllHomeWorkPapersByClassId() throws IOException {
        String classId = req.getParameter("classId");

        List<ClassPaperVO> classPaperVOS = paperService.queryAllPapersByClassIdAndType(classId, 0);

        ResponseJSONUtil.responseJSON(JsonData.buildSuccess(classPaperVOS),resp);
    }

    /**
     * 通过班级id获取发布的考试试卷
     */
    private void getAllExamPapersByClassId() throws IOException {
        String classId = req.getParameter("classId");

        List<ClassPaperVO> classPaperVOS = paperService.queryAllPapersByClassIdAndType(classId, 1);

        ResponseJSONUtil.responseJSON(JsonData.buildSuccess(classPaperVOS),resp);
    }

    /**
     * 某教师添加新试题
     */
    private void addNewPaper() throws IOException {
        JSONObject jsonObject = JSONObject.parseObject(IOUtils.toString(req.getInputStream(), "utf-8"));

        Paper paperData = jsonObject.getObject("paperObject", Paper.class);
        String paperId = jsonObject.getString("paperId");

        List<Questions> questionsList = jsonObject.getJSONArray("questionList").toJavaList(Questions.class);

        paperService.insertNewPaper(paperData,paperId,questionsList );
    }
}
